package com.example.firstapp;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.firstapp.entity.Login;
import com.example.firstapp.entity.User;
import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;
import service.LoginService;
import util.MyDatabaseHelper;
import util.SaveUser;

public class MainActivity extends AppCompatActivity {
    EditText userName;
    EditText userPassword;
    TextView message;
    Button bt_login, bt_sign;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //开启SQLiteStudioService
        SQLiteStudioService.instance().start(this);

        //链接数据库
        MyDatabaseHelper helper = new MyDatabaseHelper(this, "FistApp.db",
                null, 1);

        //获取数据库对象
        db = helper.getWritableDatabase();

        //绑定组件
        userName = findViewById(R.id.editText_name);
        userPassword = findViewById(R.id.editText_password);
        message = findViewById(R.id.TextView_message);
        bt_login = findViewById(R.id.bt_longin);
        bt_sign = findViewById(R.id.bt_sign);

        //默认登录用户
//        String defUser = "user01";
//        userName.setText(defUser);
//        userPassword.setText(SaveUser.getUserInfo(MainActivity.this, defUser));


        /*
         * 监听组件
         * 用匿名监听组件
         */
        //登录按钮监听
        bt_login.setOnClickListener(view -> {
            String name = userName.getText().toString();
            String password = userPassword.getText().toString();
            //创建用户信息实体对象，放入信息
            User user = new User(name, password);
            //调用Service层方法验证登录，将返回的数据放在登录信息实体对象中
            Login login = LoginService.login(db, user);
            //设置提示信息
            message.setText(login.getMassage());
            if (login.isState()) {//如果验证成功，跳转页面
                Intent intent = new Intent(this, TestActivity.class);
                SaveUser.saveUserInfo(MainActivity.this, user.getName(), user.getPassword());
                startActivity(intent);
            }

        });

        //注册按钮监听
        bt_sign.setOnClickListener(view -> {
            Intent intent = new Intent(this, SignActivity.class);
            startActivity(intent);
        });

        //监听用户名输入框内容变化
        userName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String user = String.valueOf(userName.getText());
//                if (!Objects.equals(SaveUser.getUserInfo(MainActivity.this,user), ""))
                userPassword.setText(SaveUser.getUserInfo(MainActivity.this, user));
            }
        });

    }

}
